package com.mingtu.guards.activity;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.StringUtils;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mingtu.common.adapter.MyFragmentPagerAdapter;
import com.mingtu.common.entity.TabEntity;
import com.mingtu.common.utils.UIUtils;
import com.mingtu.guards.R;
import com.mingtu.guards.base.MyBaseActivity;
import com.mingtu.guards.fragment.MyReportRecordFragment;
import com.mingtu.guards.fragment.MyTrackRecordFragment;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;

public class MyRecordActivity extends MyBaseActivity {

    @BindView(R.id.common_tab)
    CommonTabLayout commonTab;
    @BindView(R.id.view_pager)
    ViewPager viewPager;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {"巡护记录", "上报记录"};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_record;
    }

    @Override
    protected void initView() {

        setTitleBarBackground(getResources().getDrawable(com.mingtu.center.R.drawable.titlebar_line_bottom));
    }

    @Override
    protected void initData() {
        int navPositon = getIntent().getIntExtra("navPositon", 0);
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }

        //从首页跳转过来，查看当日的记录
        String flag = getIntent().getStringExtra("flag");

        if (!StringUtils.isEmpty(flag)) {
            Calendar startCalendar = Calendar.getInstance(); //Calendar类型的数据之后 将时间的时分秒数据重置为0,然后再转为Date型数据
            int yearS = startCalendar.get(Calendar.YEAR);
            int monthS = startCalendar.get(Calendar.MONTH);
            int dayS = startCalendar.get(Calendar.DAY_OF_MONTH);


            startCalendar.set(yearS, monthS, 1, 0, 0, 0);
            Date MyDate = startCalendar.getTime();

            String beginTime1 = UIUtils.getTime2(MyDate);
            String beginTime2 = UIUtils.getTime3(MyDate);


            Calendar endCalendar = Calendar.getInstance(); //Calendar类型的数据之后 将时间的时分秒数据重置为0,然后再转为Date型数据
            int year = endCalendar.get(Calendar.YEAR);
            int month = endCalendar.get(Calendar.MONTH);
            int day = endCalendar.get(Calendar.DAY_OF_MONTH);

            int hour = endCalendar.get(Calendar.HOUR_OF_DAY);
            int minute = endCalendar.get(Calendar.MINUTE);
            int second = endCalendar.get(Calendar.SECOND);

            startCalendar.set(year, month, day, hour, minute, second);
            Date endDate = startCalendar.getTime();
            String endTime1 = UIUtils.getTime2(endDate);
            String endTime2 = UIUtils.getTime3(endDate);

            mFragments.add(MyTrackRecordFragment.getInstance(beginTime1, endTime1));
            mFragments.add(MyReportRecordFragment.getInstance(beginTime2, endTime2));
            setModuleTitle("本月记录");
        } else {

            mFragments.add(MyTrackRecordFragment.getInstance());
            mFragments.add(MyReportRecordFragment.getInstance());
            setModuleTitle("我的记录");
        }


        viewPager.setOffscreenPageLimit(2);//需要设置此值
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments));
        viewPager.setCurrentItem(navPositon);
        viewPager.addOnPageChangeListener(new MyOnPageChangeListener());

        commonTab.setTextBold(1);
        commonTab.setTabData(mTabEntities);
        commonTab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        commonTab.setCurrentTab(navPositon);


    }

    @Override
    protected void getData() {

    }

    private class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            commonTab.setCurrentTab(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

    }

}